
//**************************************
//INCLUDE files for :Hexadecimal converter: Converts hex to original ASCII characters [CGI/SERVER] related.
//**************************************
//There are no include files. 

//**************************************
// Name: Hexadecimal converter: Converts hex to original ASCII characters [CGI/SERVER] related.
// Description:The first version of this had a few undeclared
//variables and no interface. I have corrected that.
//It is now even better than before.
//To convert hexadecimal values from a HTML FORM 
//after it is posted to their original ASCII or you 
//can just use it to suit your needs.
//characters.
// By: Koda
//
//
// Inputs:A char pointer.
//
// Returns:a char pointer
//
//Assumes:Both functions have to work together.
//If any one needs the entire Parsing Class 
//or just have questions contact
//me at the above email.
//It was built to be compiled and placed in a SERVER.
//Use it with a Post mehtod from a form.
//
//Side Effects:(IT MAY WORK TOO WELL. YOU MIGHT LIKE IT TOO MUCH)
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.1932/lngWId.3/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

//(c) Koda
// Jul.13.2001
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <iomanip>
class HEX


    {
    public:
    	HEX(){ ; }
    ~HEX();
    	char *temp_data;
    char *out;
};
HEX::~HEX()


    {
    delete [] temp_data;
    delete [] out;
}

int ahextoi( char *value )


    {
    	int local_index = 0, dec_value = 0;
    while( *(value + local_index ))


        {
        if((*(value + local_index ) >= '0' ) && (*(value + local_index ) <= '9' ))


            {
            	dec_value = ( (*(value + local_index ) - '0' )+
            (dec_value * 16 ));
        }
        else


            {
            *( value + local_index ) = toupper( *( value + local_index ) );
            	dec_value = (((*(value + local_index ) - 'A') + 10 )
            + ( dec_value * 16 ));
        }
        local_index++;
    }
    return (dec_value);
}
char * clear_hex(const char *client_data, HEX &h )


    {
    h.temp_data = new char[ strlen( client_data ) ];
    char value[3];
    int index = 0;
    
    while( *client_data)


        {
        if( *client_data == '%' )


            {
            		value[0] = * ( client_data + 1 );
            value[1] = * ( client_data + 2 );
            value[2] = 0;
            h.temp_data[ index++ ] = ahextoi( value );
            client_data += 3;
        }
        else


            {
            h.temp_data[ index++ ] = *client_data;
            client_data++;
        }
        
    }
    h.temp_data[ index ] = 0;
    h.out = new char[ strlen( h.temp_data ) ];
    strcpy( h.out, h.temp_data );
    
    	return (h.out);
}
/*void main()


    {
    HEX h;
    char hex[4] = {NULL};
    cout <<"Enter the hexadecimal value preceeded by the modulos(%) sign:";
    cin >> setw( 4 ) >> hex;
    cout<< "The ASCII character is "<< clear_hex( hex, h )<< endl;
}*/

char hextochr(std::string hex)
{
	HEX h;
	return clear_hex(hex.c_str(), h)[0];
}
